package com.example.springboot_shiro.config;


import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.example.springboot_shiro.realm.AccountRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Hashtable;
import java.util.Map;

@Configuration
public class ShiroConfig {

  @Bean
  public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager securityManager){
    ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
    factoryBean.setSecurityManager(securityManager);
    //权限设置
    Map<String,String> map = new Hashtable<>();
    map.put("/main","authc");
    map.put("/manage","perms[manage]");
    map.put("/administrator","roles[administrator]");
    factoryBean.setFilterChainDefinitionMap(map);
    //设置登录界面
    factoryBean.setLoginUrl("/login");
    //设置未授权
    factoryBean.setUnauthorizedUrl("/unauth");
    return factoryBean;
  }

  /**
   * @Qualifier("accountRealm")中的名字就是调用的那个方法的方法名
   *
   * */
  @Bean
  public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("accountRealm") AccountRealm accountRealm){
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(accountRealm);
    return manager;
  }
/**
 * 先把AccountRealm装入ioc中，然后创建一个manager,
 * 把自己创建的bean装入到manager中，再创建一个factoryBean，
 *把manager装入到factoryBean中。
 * */
  @Bean
  public AccountRealm accountRealm(){
    return new AccountRealm();
  }

  @Bean
  public ShiroDialect shiroDialect(){
    return new ShiroDialect();
  }
}
